{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy矩阵及其运算\n",
    "## 一.矩阵的创建\n",
    "## 二.矩阵的运算\n",
    "# numpy统计分析\n",
    "## 三.NumPy读取和存储文件\n",
    "## 四.NumPy常用统计函数\n",
    "## 知识目标：掌握矩阵的创建方法及运算\n",
    "## 技能目标：掌握通用函数的使用\n",
    "## 重点：矩阵的创建及通用函数的使用\n",
    "## 难点：矩阵的运算\n",
    "\n",
    "=========================================================\n",
    "\n",
    "【上周回顾】\n",
    "#NumPy的引用：import numpy as np\n",
    "#ndarray的属性：ndim、shape、size、dtype、itemsize  \n",
    "#ndarray数组的创建：np.array()、np.arange(n) 、np.ones(shape)、np.zeros(shape)、\n",
    "                                 np.full(shape,val) 、np.linspace() 、np.logspace() ……\n",
    "#ndarray数组的变换：.reshape(shape) 、.ravel () 、.hstack()、.vstack()\n",
    "#ndarray数组的索引和切片：\n",
    "\n",
    "一维 a[n]、     a[起止编号：终止编号（不含）：步长]    \n",
    "                                          \n",
    "                                          \n",
    "多维 a[x,y,z]     a[x:x1:步长,  y:y1:步长,  z:z1:步长]   \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一.矩阵的创建\n",
    "### 1.使用mat或matrix函数创建矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "matr1 = np.mat(\"1,2,3;4,5,6;7,8,9\")   #数据之间用分号隔开\n",
    "matr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "matr2 = np.matrix([[9,8,7],[6,5,4],[3,2,1]])\n",
    "matr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "mat与matrix的区别：\n",
    "使用mat函数创建矩阵时，若输入ndarray对象时，则不会为它们创建副本，因此，调用mat函数和调用matrix(data,copy=False)等价"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.使用bmat函数创建矩阵\n",
    "将小矩阵组合成大矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.bmat(\"matr1,matr2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### *矩阵与数组的区别：\n",
    "#数组array与矩阵matrix是NumPy中两种不同的类型，Numpy arrays 可以是多维的，但是 Numpy matrices必须是二维的。matrix是array的一个小的分支，包含于Array。所以matrix 拥有array的所有特性。\n",
    "#array与matrix执行相同的数学运算会得到不同的结果： 数组之间相乘，是元素与对应位置的元素逐一相乘；矩阵之间相乘得到的是矩阵积。\n",
    "#运算符 ** 表示的含义也不同，对于maxtrix来说，a ** 2表示a * a，对于array来说，a ** 2表示数组里的数的平方。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#数组相乘\n",
    "a1=np.array([[1,2],[3,4]])\n",
    "a2=np.array([[5,6],[7,8]])\n",
    "a3 = a1 * a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#矩阵相乘\n",
    "m1=np.mat(\"1,2;3,4\")\n",
    "m2=np.mat(\"5,6;7,8\")\n",
    "m3 = m1 * m2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拓展：矩阵积怎么运算出来的？（详见word文档）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二.矩阵的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#矩阵与数相乘 \n",
    "m1*3\n",
    "#矩阵相加减 \n",
    "m1+m2\n",
    "m1-m2\n",
    "#矩阵相乘 \n",
    "m1*m2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#矩阵对应元素相乘\n",
    "np.multiply(m1,m2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#数组相乘，若想得到矩阵积\n",
    "a1.dot(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#矩阵转置\n",
    "matrix.T    #返回自身的转置（m行n列变成n行m列）\n",
    "matrix.H   #返回自身的共轭转置\n",
    "matrix.I   #返回自身的逆矩阵\n",
    "matrix.A   #返回自身数据的二维数组的一个视图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三.NumPy读取和存储文件\n",
    "### 1.读/存 二进制文件\n",
    "#### （1）存储二进制文件\n",
    "np.sava(file, arr) 或 np.savez(file, arr1, arr2)\n",
    "\n",
    "file：文件名，单个数组以.npy为扩展名，存储多个数组压缩拓展名为.npz\n",
    "\n",
    "存储时扩展名可以省，但读取时不能省。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(50).reshape(5, 2, 5)\n",
    "np.save('D:/pyData/a1', arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2 = np.array([[1,2],[3,4]])\n",
    "np.savez('D:/pyData/b1', arr1, arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### (2)读取二进制文件\n",
    "np.load(file)      注意：读取时不能省扩展名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.load('D:/pyData/a1.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.load('D:/pyData/b1.npz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = np.load('D:/pyData/b1.npz')\n",
    "data1['arr_0']     #arr_0表示压缩文件中第一个数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.读/存 csv或txt文件\n",
    "#### （1）存储csv或txt文件\n",
    "CSV（逗号分隔值）只能存一维和二维数组\n",
    "\n",
    "np.savetxt ( fname, array, fmt='%.18e', delimiter=None )\n",
    "\n",
    "fname：文件或字符串的名字，可以是.gz或.bz2的压缩文件的名字。\n",
    "\n",
    "array：存入文件的数组。\n",
    "\n",
    "fmt：写入文件中每个元素使用的格式，如：%d 、%.2f、%1.8e\n",
    "\n",
    "delimiter：字符串之间的分隔符，默认是空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr3 = np.arange(50).reshape(5,10)\n",
    "np.savetxt ( 'D:/pyData/c3', arr3, fmt='%d', delimiter=',' )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### (2) 读取csv或txt文件\n",
    "np.loadtxt( fname, dtype=np.float, delimiter=None, unpack=False )\n",
    "\n",
    "unpack：读入属性将分别写入不同数组变量，一列数据会组成一行，相当于转置的效果，False读入数据只写入一个数组变量，有多少条数据就有多少行，默认False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.loadtxt ('D:/pyData/c3',  delimiter=',' , unpack=True )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四.NumPy常用统计函数\n",
    "### 1.排序（sort函数）\n",
    "np.sort(arr, axis=1, kind='quicksort', order=None)\n",
    "\n",
    "arr：需要排序的数组\n",
    "\n",
    "axis：指定数据排序的轴；axis=1为沿横轴排序； axis=0为沿纵轴排序\n",
    "\n",
    "kind：数组排序时使用的方法，默认kind= 'quicksort' 为快排\n",
    "\n",
    "order：一个字符串或列表，可以设置按照某个属性进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#不加任何参数的直接排列\n",
    "np.random.seed(1)   #设置随机种子  伪随机\n",
    "arr4=np.random.randint(1, 42, size=10)\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sort(arr4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#参数axis的使用，按指定轴排列\n",
    "arr5=np.random.randint(1,40,size=(3,3))\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr5.sort(axis=0)\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr5.sort(axis=1)\n",
    "arr5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "思考：如果先按axis=1排序，再按axis=0排序，结果是一样吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#参数order的使用，指定排列规则\n",
    "list1 = [('Name', 'S10'), ('Height', float), ('Age', int)]\n",
    "values = [('Li', 1.8, 41), ('Wang', 1.9, 38),('Duan', 1.7, 38)]\n",
    "arr6 = np.array(values, dtype=list1)\n",
    "np.sort(a, order='Height')      # 按照属性Height进行排序,此时参数为字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sort(a, order=['Age', 'Height'])    \n",
    "# 先按照属性Age排序,如果Age相等，再按照Height排序，此时参数为列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.去重 （unique函数）\n",
    "np.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "names = np.array(['张三风','张五忌','李达','王其木','张五忌','李达'])\n",
    "np.unique(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.常用统计函数\n",
    "np.sum (a, axis =None或0或1) :      计算数组的和\n",
    "\n",
    "np.mean (a, axis =None或0或1) :     计算数组的平均值\n",
    "\n",
    "np.std (a, axis =None或0或1) ：     计算数组的标准差\n",
    "\n",
    "np.var (a, axis =None或0或1) :      计算数组的方差\n",
    "\n",
    "np.min (a, axis =None或0或1)：      计算数组的最小值\n",
    "\n",
    "np.max (a, axis =None或0或1)：      计算数组的最大值\n",
    "\n",
    "np.cumsum (a, axis =None或0或1)：    计算数组的累计和\n",
    "\n",
    "np.cumprod (a, axis =None或0或1)：   计算数组的累计积\n",
    "\n",
    "np.average(a, axis =None或0或1,weights=None或[指定权重])：计算数组a相关元素的加权平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr7 = np.arange(15).reshape(3, 5) \n",
    "np.sum(arr7,axis=0)\n",
    "np.mean(arr7,axis=1)\n",
    "np.std(arr7,axis=0)\n",
    "np.var(arr7,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.average(arr7, axis =0, weights =[10, 5, 1])\n",
    "#解析：1.对a在0轴上各元素加权求平均，weights中为权重，注意要和a的第一维匹配\n",
    "   #  2.以4.1875为例，(2*10+7*5+12*1)  /  (10+5+1)=4.1875"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
